﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Pay.Token.Controllers
{
    /// <summary>
    /// token 钱包
    /// </summary>
    [AppAuthorize("Member"), RoutePrefix("token_bill")]
    public class TokonBillController :   BaseController {
        private ITokenBillService _iTokenBillService;
        private ITokenTransferService _iTokenTransferService;
        private IBillService _iBillService;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="iTokenBillService"></param>
        /// <param name="iTokenTransferService"></param>
        /// <param name="iBillService"></param>
        public TokonBillController(ITokenBillService iTokenBillService, ITokenTransferService iTokenTransferService, 
                                   IBillService iBillService) {
            _iTokenBillService = iTokenBillService;
            _iTokenTransferService = iTokenTransferService;
            _iBillService = iBillService;
        }
        /// <summary>
        /// 钱包
        /// </summary>
        /// <returns></returns>
        [Route("get_wallet"), HttpGet]
        public ResultMessage<List<token_wallet_dto>> GetWallet() {
            var result = _iTokenTransferService.GetWallet(UserId);
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 钱包详情
        /// </summary>
        /// <param name="type">1 分销 2 课程 3 店铺</param>
        /// <returns></returns>
        [Route("get_wallet_detail"), HttpGet]
        public ResultMessage<token_wallet_detail_dto> GetWalletDetail (int type) {
            var result = _iTokenTransferService.GetWalletDetail(UserId,type);
            return ResultToJson.Json(result);
        }
       /// <summary>
       /// 收益列表
       /// </summary>
       /// <param name="query"></param>
       /// <returns></returns>
        [Route("get_bill_details"), HttpGet]
        public ResultMessage<PagedResult<token_wallet_bill_detail_dto>> GetBillDetails([FromUri]WalletQuery query) {
            query.merchant_id = UserId;
            var result = new PagedResult<token_wallet_bill_detail_dto>();
            if (query.type == 2) {
                result = _iTokenBillService.GetWalletBillDetails(query);
            } else {
                result = _iBillService.GetWalletBillDetails(query);
            }
            return ResultToJson.Json(result);
        }
        /// <summary>
        /// 提现
        /// </summary>
        /// <param name="type">类型1：分销，2：店铺，3：课程</param>
        /// <param name="amount"></param>
        /// <returns></returns>
        [Route("transfer"), HttpGet]
        public ResultMessage<bool> Transfer(int type, decimal amount) {
            int result = _iTokenTransferService.ApplyTokenTransfer(UserId, type, amount);
            if (result > 0)
                return ResultToJson.Json("提现成功", true);
            else
                return ResultToJson.Json<bool>("提现失败");
        }
        /// <summary>
        /// 提现记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("deposited"), HttpGet]
        public ResultMessage<PagedResult<token_deposited_dto>> GetDeposited([FromUri] WalletQuery query) {
            query.merchant_id = UserId;
            var result = _iTokenTransferService.GetDeposited(query);
            return ResultToJson.Json(result);
        }
    }
}
